/*
 * @Author: szx
 * @Date: 2021-12-21 16:51:50
 * @LastEditTime: 2021-12-21 16:58:14
 * @Description:
 * @FilePath: \leetcode\100-199\110\110.js
 */

/**
 * @param {TreeNode} root
 * @return {boolean}
 */
var isBalanced = function (root) {
    if (!root) return true;
    maxDepth = (root) => {
        if (!root) return 0;
        return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
    };
    const leftDepth = maxDepth(root.left);
    const rightDepth = maxDepth(root.right);
    if (leftDepth - rightDepth > 1 || leftDepth - rightDepth < -1) return false;
    return isBalanced(root.left) && isBalanced(root.right);
};
